﻿using Ruici.BLL;
using RuiciWebSite.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ruici.Web.Services.ServiceForWeb;

namespace RuiciWebSite.Controllers
{
    public class ForgetPwdController : Controller
    {
        UserBll usersSevice = new UserBll();

        // GET: ForgetPwd
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 获取手机验证码
        /// </summary>
        /// <param name="regUser"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetValidateCode(RegisterViewModel regUser)
        {
            WebAccount account = usersSevice.getWebAccountByMobile(regUser.Mobile);

            if (account == null)
                return Json(new ReturnCode(-1, "手机号码未注册！"), JsonRequestBehavior.AllowGet);
            else
            {
                Random ran = new Random();
                int RandKey = ran.Next(100000, 999999);
                Session["ValidationCode"] = RandKey;
                Session["ValidationCodeGenerateTime"] = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                usersSevice.SendMessage(regUser.Mobile, Session["ValidationCode"].ToString());
                return Json(new ReturnCode(1, "验证码已发送，请注意查收!"), JsonRequestBehavior.AllowGet);
            }
            return Json(new ReturnCode(-1, "获取验证码失败，请重试"), JsonRequestBehavior.AllowGet);

        }
        /// <summary>
        /// 下一步
        /// </summary>
        /// <param name="regUser"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GoNextStep(RegisterViewModel regUser)
        {
            if(Session["ValidationCode"]==null)
                return Json(new ReturnCode(-2, "请点击获取验证码"), JsonRequestBehavior.AllowGet);

            DateTime ValCodeGenTime = Convert.ToDateTime(Session["ValidationCodeGenerateTime"]);
            TimeSpan ts1 = new TimeSpan(ValCodeGenTime.Ticks);
            TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
            TimeSpan ts = ts1.Subtract(ts2).Duration();
            if (ts.Minutes <= 10)
            {
                if (regUser.ValidateCode != Session["ValidationCode"].ToString())
                    return Json(new ReturnCode(-1, "请输入正确的手机验证码"), JsonRequestBehavior.AllowGet);
                else
                {
                    Session["ValidationCode"] = string.Empty;
                    Session["ValidationCodeGenerateTime"] = string.Empty;
                }
            }
            else
                return Json(new ReturnCode(-1, "手机验证码已过期"), JsonRequestBehavior.AllowGet);

            WebAccount account = usersSevice.getWebAccountByMobile(regUser.Mobile);
            
            if(account==null)
                return Json(new ReturnCode(-1, "手机号码未注册！"), JsonRequestBehavior.AllowGet);

            return Json(new ReturnCode(1, ""), JsonRequestBehavior.AllowGet);

        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="regUser"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult ChangePassword(RegisterViewModel regUser)
        {
            Dictionary<int,string> UpdateResultDic = new Dictionary<int, string>();

            if (regUser == null)
                return Json(new ReturnCode(-1, "请检查是否填写了您的个人信息。"), JsonRequestBehavior.AllowGet);

            WebAccount account = usersSevice.getWebAccountByMobile(regUser.Mobile);          
            if (account != null)
            {
                Session["isLogin"] = true;
                Session["pid"] = account.PID;
                Session["userName"] = account.Name;
                Session["mobile"] = account.Mobile;

            }

            UpdateResultDic = usersSevice.changePassword(account.UserName, regUser.Password);

            return Json(new ReturnCode(UpdateResultDic.First().Key,UpdateResultDic.First().Value), JsonRequestBehavior.AllowGet);

        }
    }
}